package com.fly.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fly.Common.R;
import com.fly.pojo.Flight;
import com.fly.service.FlightService;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/flight")
public class FlightController {
    @Autowired
    private FlightService flightService;

    /**
     * 返回后台所有航班信息
     * @param page
     * @param
     * @return
     */
    @GetMapping("/getFlight")
    public R<Page> GetFlight( int page,int pageSize){
        Page<Flight> flightPage =new Page<>(page,pageSize);

        flightService.page(flightPage);
        System.out.println(flightPage.toString());

        return R.success(flightPage);
    }

    /**
     * 查询指定航班信息并返回
     * @param id
     * @return
     */
    @GetMapping("/getData")
    public  R<Flight> GetDet(Long id){
        Flight flight =Get(id);
        return R.success(flight);
    }

    /**
     * 用于获取指定航班信息
     * @param id
     * @return
     */
    public Flight Get(Long id){
        LambdaQueryWrapper<Flight> queryWrapper =new LambdaQueryWrapper<Flight>();
        queryWrapper.eq(Flight::getFlightId,id);
        return flightService.getOne(queryWrapper);
    }

    /**
     * 获取指定的修改航班信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public  R<Flight> GetUpdate(@PathVariable Long id){
        System.out.println(id);
        Flight flight =Get(id);
        return R.success(flight);

    }

    /**
     * 更新修改后的航班信息
     * @param flight
     * @return
     */
    @PostMapping("/update")
    public R<String> UpdateFlight(Flight flight){
        LambdaQueryWrapper<Flight> queryWrapper =new LambdaQueryWrapper<Flight>();
        queryWrapper.eq(Flight::getFlightId,flight.getFlightId());
        flightService.update(flight,queryWrapper);
        return R.success("更新成功");
    }

    @PostMapping("/save")
    public  R<String> SaveFlight(Flight flight){

        flightService.save(flight);
        return R.success("更新成功");
    }
    @DeleteMapping("/{ids}")
    public  R<String> DelFlight(@PathVariable List<Integer> ids){

        flightService.DeleteFlight(ids);


        return R.success("删除成功");
    }


}
